package com.baomidou.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.springboot.db.entity.WxTokenObj;
import com.baomidou.springboot.repository.IWxTokenRepository;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 代码生成器，参考源码测试用例：
 * <p>
 * /mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/generator/MysqlGenerator.java
 */
@RestController
@RequestMapping("/wx")
public class WxController {

    private Gson gson = new Gson();

    @Autowired
    private IWxTokenRepository wxTokenRepository;

    @GetMapping(value = "/id/{id}")
    public String getById(@PathVariable String id) {
        WxTokenObj wxTokenObj = wxTokenRepository.getToken(id);

        return gson.toJson(wxTokenObj);
    }

    @GetMapping("/token/{token}")
    public String getByToken(@PathVariable String token) {
        Wrapper<WxTokenObj> wrapper = new QueryWrapper<WxTokenObj>().lambda().eq(WxTokenObj::getToken, token);

        WxTokenObj wxTokenObj = wxTokenRepository.getToken(wrapper);

        return gson.toJson(wxTokenObj);
    }

    @GetMapping("/token2/{token}")
    public String getByToken2(@PathVariable String token) {
        Wrapper<WxTokenObj> wrapper = new QueryWrapper<WxTokenObj>().lambda().eq(WxTokenObj::getToken, token);

        WxTokenObj wxTokenObj = wxTokenRepository.selectOne(wrapper);

        return gson.toJson(wxTokenObj);
    }

    @DeleteMapping("/id/{id}")
    public String deleteById(@PathVariable String id) {
        boolean success = wxTokenRepository.deleteById(id);

        return gson.toJson(success);
    }

}
